<?php
class omeauto_autodispatch{
    
    function get_conditions(){
        return kernel::single('omeauto_auto')->get_conditions('autodispatch');
    }
    
    function autodispatch($order_sdf){
        $order_id = $order_sdf['order_id'];
        
        $oAutodispatch = &app::get('omeauto')->model('autodispatch');
        $oOrder = &app::get('ome')->model('orders');

        ////////判断订单是否为销售单，如果是那么返回FALSE不与合并//////////
        $o = $oOrder->dump($order_id,'order_type');
        if ($o['order_type'] == 'sale' || $o['is_fail'] == 'true') return false;
        //////////////////////////////////////////////////////////////////////
        
        $rows = $oAutodispatch->getList("*",array(),0,-1);
        if($rows){
            $group_id = "";
            $op_id = "";
            foreach($rows as $row){
                $flag = false;
                $config = $row['config'];
                if($config){
                    foreach($config as $condition=>$value){
                        $oCondition = kernel::single($condition);
                        foreach($value as $v){
                            $flag = $oCondition->validate($order_sdf,$v);
                            if(!$flag){           
                                break;
                            }
                        }
                        if(!$flag){
                            break;
                        }
                    }
                }
                if($flag){
                    $group_id = $row['group_id'];
                    $op_id = $row['op_id'];
                    break;
                }
            }
            
            if($group_id){
                $data = array('group_id'=>$group_id,'op_id'=>$op_id,'dt_begin'=>time(),'dispatch_time'=>time());
                $oOrder->dispatch($data,array('order_id'=>$order_id),array($order_id),true);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}